const logger = require('log4js').getLogger();

module.exports = (req, res) => {
  require('../util/service').handlePost(req, res, (reqObj, resolve) => {
    if (isNaN(parseInt(reqObj.html_id))) {
      resolve({
        result: -1,
        msg: '缺少html_id'
      })
      return
    }
    if (!reqObj.html) {
      resolve({
        result: -1,
        msg: '缺少html'
      })
      return
    }
    require('../dao/htmlDao').select({
      html_id: reqObj.html_id
    }, htmls => {
      if (htmls.length == 0) {
        resolve({
          result: -1,
          msg: '没有找到html'
        })
        return
      }
      var html = htmls[0]
      html.html = reqObj.html;
      html.time = new Date();
      html.needLogin = reqObj.needLogin;
      html.isMarkdown = reqObj.isMarkdown;
      require('../dao/htmlDao').update(html, () => {
        resolve({
          result: 1
        })
      }, result => {
        logger.error('['+  __filename + ':' + __line+']' + this.host + ':' + this.port, result);
        resolve({
          result: -1,
          msg: '系统错误'
        })
      })
    })
  })
}
